package com.xkcyy.one.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xkcyy.one.dto.UserDTO;
import com.xkcyy.one.entity.User;
import com.xkcyy.one.exception.BusinessException;
import com.xkcyy.one.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户服务实现类
 *
 * @author AI Assistant
 */
@Service
public class UserService {
    
    private final UserMapper userMapper;
    
    @Autowired
    public UserService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
    
    
    public List<UserDTO> getAllUsers() {
        return userMapper.selectList(null).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    
    public UserDTO getUserById(Long id) {
        User user = userMapper.selectById(id);
        if (user == null) {
            throw new BusinessException("用户不存在，ID: " + id);
        }
        return convertToDTO(user);
    }
    
    
    public UserDTO getUserByUsername(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        User user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            throw new BusinessException("用户不存在，用户名: " + username);
        }
        return convertToDTO(user);
    }
    
    
    public UserDTO createUser(User user) {
        // 检查用户名是否已存在
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername());
        if (userMapper.selectCount(queryWrapper) > 0) {
            throw new BusinessException("用户名已存在: " + user.getUsername());
        }
        
        // 检查邮箱是否已存在
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getEmail, user.getEmail());
        if (userMapper.selectCount(queryWrapper) > 0) {
            throw new BusinessException("邮箱已存在: " + user.getEmail());
        }
        
        // 设置创建时间和更新时间
        LocalDateTime now = LocalDateTime.now();
        user.setCreateTime(now);
        user.setUpdateTime(now);
        
        // 插入用户
        userMapper.insert(user);
        
        return convertToDTO(user);
    }
    
    
    public UserDTO updateUser(User user) {
        // 检查用户是否存在
        User existingUser = userMapper.selectById(user.getId());
        if (existingUser == null) {
            throw new BusinessException("用户不存在，ID: " + user.getId());
        }
        
        // 检查用户名是否已被其他用户使用
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername())
                .ne(User::getId, user.getId());
        if (userMapper.selectCount(queryWrapper) > 0) {
            throw new BusinessException("用户名已存在: " + user.getUsername());
        }
        
        // 检查邮箱是否已被其他用户使用
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getEmail, user.getEmail())
                .ne(User::getId, user.getId());
        if (userMapper.selectCount(queryWrapper) > 0) {
            throw new BusinessException("邮箱已存在: " + user.getEmail());
        }
        
        // 设置更新时间
        user.setUpdateTime(LocalDateTime.now());
        
        // 更新用户
        userMapper.updateById(user);
        
        return convertToDTO(user);
    }
    
    
    public void deleteUser(Long id) {
        // 检查用户是否存在
        User existingUser = userMapper.selectById(id);
        if (existingUser == null) {
            throw new BusinessException("用户不存在，ID: " + id);
        }
        
        // 删除用户
        userMapper.deleteById(id);
    }
    
    /**
     * 将实体转换为DTO
     *
     * @param user 用户实体
     * @return 用户DTO
     */
    private UserDTO convertToDTO(User user) {
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user, userDTO);
        return userDTO;
    }
}
